package org.summer.generator.writer.impl;

import org.summer.generator.StringUtil;
import org.summer.generator.log.Logger;
import org.summer.generator.log.LoggerManager;
import org.summer.generator.vo.MethodInfo;
import org.summer.generator.writer.AbstractJavaFileWriter;

import java.util.List;

/**
 * 数据库操作数据层接口文件写入处理类
 */
public class RepositoryImplFileWriter extends AbstractJavaFileWriter {
    private static final Logger log = LoggerManager.getLogger(RepositoryImplFileWriter.class);
    @Override
    public void execute() {
        String packageName = project.getRepositoryPackageName();
        String packageDefinition = getPackageDefinition(packageName + ".impl");
        String classDefinition = getClaasImplDefinition(packageName);
        String dependencyClass = getDependencyClass(project.getMapperPackageName());
        List<MethodInfo> mds = parseMethodDefinitions(writer.getRepositoryParentInterface());
        channel.writerNewline(packageDefinition);
        channel.writerEmptyLine();
        channel.writerNewline(IMPORT_LOMBOK_RAC);
        channel.writerNewline("import org.springframework.stereotype.Repository;");
        importSet.forEach(channel::writerNewline);
        channel.writerNewline(table.getEntityImportStr());
        channel.writerEmptyLine();
        channel.writerNewline(CLASS_REMARK.formatted(table.getRemark() + "数据层操作接口实现类"));
        channel.writerNewline("@Repository");
        channel.writerNewline(LOMBOK_RAC);
        channel.writerNewline(classDefinition);
        channel.writerNewline(FIELD_PREFIX + dependencyClass + " " + StringUtil.firstLetterLowerCase(dependencyClass) + LINE_END);
        mds.forEach(methodInfo -> writerMethod(methodInfo,StringUtil.firstLetterLowerCase(dependencyClass), null));
        channel.writerEmptyLine();
        channel.writerNewline(BLOCK_END);
    }

    @Override
    protected void writerMethod(MethodInfo method, String previousObject, String loadMethodName) {
        channel.writerEmptyLine();
        StringBuilder builder = new StringBuilder(METHOD_PREFIX);
        builder.append(method.getReturnType()).append(SPACING);
        builder.append(method.getMethodName());
        builder.append("(").append(method.getParamTypeAndName()).append(")");
        builder.append(BLOCK_START).append(LINE);
        builder.append(parseReturnBody(method,previousObject));
        builder.append(TAB).append(BLOCK_END);
        channel.writerNewline(builder.toString());
    }

    /**
     * 解析方法返回体内容
     */
    private String parseReturnBody(MethodInfo method, String previousObject){
        String mn = method.getMethodName();
        if (mn.startsWith(REPOSITORY_SEARCH)){
            mn = mn.replace(REPOSITORY_SEARCH, MAPPER_SELECT);
        }
        if (mn.startsWith(REPOSITORY_APPEND)){
            mn = mn.replace(REPOSITORY_APPEND, MAPPER_INSERT);
        }
        if (mn.startsWith(REPOSITORY_CANCEL)){
            mn = mn.replace(REPOSITORY_CANCEL, MAPPER_DELETE);
        }
        if (mn.startsWith(REPOSITORY_EDIT)){
            mn = mn.replace(REPOSITORY_EDIT, MAPPER_UPDATE);
        }
        return METHOD_RETURN_PREFIX + previousObject
                + "." + mn + "(" + method.getParamName() + ");\n";
    }

}
